package com.cnzz.site1255174697.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.alibaba.baichuan.android.trade.AlibcTrade;
import com.alibaba.baichuan.android.trade.AlibcTradeSDK;
import com.alibaba.baichuan.android.trade.callback.AlibcTradeCallback;
import com.alibaba.baichuan.android.trade.constants.AlibcConstants;
import com.alibaba.baichuan.android.trade.model.AlibcShowParams;
import com.alibaba.baichuan.android.trade.model.OpenType;
import com.alibaba.baichuan.android.trade.model.TradeResult;
import com.alibaba.baichuan.android.trade.page.AlibcBasePage;
import com.alibaba.baichuan.android.trade.page.AlibcDetailPage;
import com.alibaba.baichuan.android.trade.page.AlibcPage;
import com.cnzz.site1255174697.R;
import com.cnzz.site1255174697.base.BaseActivity;
import com.cqyanyu.yanyu.model.EventEntity;
import com.cqyanyu.yanyu.utils.XToastUtil;
import com.cqyanyu.yanyu.view.FontIconView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

/**
 * 通过Id打开详情
 */
public class WebItemIdActivity extends BaseActivity {

    protected WebView webView;
    protected String titlet;
    private String url1;
    protected FontIconView fiTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_web);
        initView();
        Intent intent = getIntent();
        setTitle("加载中...");
        if (intent == null) {
            XToastUtil.showToast(this, "请传入参数");
        } else {
            url1 = intent.getStringExtra("url");
            titlet = intent.getStringExtra("title");
            if (titlet != null && !titlet.equals("")) {
                fiTitle.setText(titlet);
            }
        }
        load();
        EventBus.getDefault().register(this);
    }

    private void load() {
        //提供给三方传递配置参数
        Map<String, String> exParams = new HashMap<>();
        exParams.put(AlibcConstants.ISV_CODE, "appisvcode");
        AlibcTradeSDK.setForceH5(true);
        //实例化URL打开page
        AlibcBasePage detailPage = new AlibcDetailPage(url1);
        //设置页面打开方式
        AlibcShowParams showParams = new AlibcShowParams(OpenType.H5, false);
        //使用自己的Activity & webview打开detail
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        //同步Cookie
        AlibcTrade.show(this, webView, new WebViewClient() {
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        if (url.startsWith("tel:")) {
                            Intent intent = new Intent(Intent.ACTION_VIEW,
                                    Uri.parse(url));
                            startActivity(intent);
                            return true;
                        }
                        if (url.startsWith("https://pass.tmall.com/add")) {
                            //天猫商品
                            EventEntity eventEntity = new EventEntity();
                            eventEntity.type = 222;
                            EventBus.getDefault().post(eventEntity);
                        } else if (url.startsWith("https://login.m.tmall.com/")) {
                            EventEntity eventEntity = new EventEntity();
                            eventEntity.type = 222;
                            EventBus.getDefault().post(eventEntity);
                        } else if (url.startsWith("https://login.m.etao.com")) {
                            EventEntity eventEntity = new EventEntity();
                            eventEntity.type = 222;
                            EventBus.getDefault().post(eventEntity);
                        }
                        if (url.startsWith("http:") || url.startsWith("https:")) {
                            view.loadUrl(url);
                            return true;
                        }
                        return true;
                    }
                }, new WebChromeClient() {
                    @Override
                    public void onReceivedTitle(WebView view, String title) {
                        super.onReceivedTitle(view, title);
                        if (WebItemIdActivity.this.titlet == null) {
                            fiTitle.setText(title);
                        }
                    }
                }, detailPage, showParams, null, exParams,
                new AlibcTradeCallback() {
                    @Override
                    public void onTradeSuccess(TradeResult tradeResult) {
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                    }
                });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(EventEntity eventEntity) {
        CookieSyncManager.createInstance(WebItemIdActivity.this);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        if (eventEntity != null && eventEntity.type == 6666) {
            cookieManager.setCookie(url1, eventEntity.state);
        } else if (eventEntity != null && eventEntity.type == 9999) {
            cookieManager.setCookie(url1, eventEntity.state);
        }
        CookieSyncManager.getInstance().sync();
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();   //后退
        } else {
            super.onBackPressed();
        }
    }

    private void initView() {
        webView = (WebView) findViewById(R.id.mWebView);
        fiTitle = (FontIconView) findViewById(R.id.fi_title);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
